﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BitworkStudios.Scraper.Document;
using CsvHelper;

namespace BitworkStudios.Scraper
{
    public class WebScraper<T>
    {
        public Uri DocumentUri { get; set; }
        public Structure<T> DocumentStructure { get; set; }
        
        public WebScraper(string documentUrl)
        {
            DocumentUri = new Uri(documentUrl);
            DocumentStructure = new Structure<T>();
        }

        public void Scrape()
        {
            var webClient = new System.Net.WebClient();
            string documentHtml = webClient.DownloadString(DocumentUri);
            DocumentStructure.Scrape(documentHtml);
        }

        public static void CreateCsvTemplate<T>(string filePath)
        {
            using (var csvWriter = new CsvWriter(new StreamWriter(filePath)))
            {
                csvWriter.WriteHeader<Element>();
                csvWriter.WriteRecords(Element.GetTemplate<T>());
            }
        }
    }
}
